<?php
$OJ_CACHE_SHARE=false;
$cache_time=30;
require_once("config/path_config.php");
require_once('./include/cache_start.php');
require_once('./include/db_info.inc.php');
require_once('./include/setlang.php');
require_once ('./common/time_utils.php');
require_once('./config/sys_config.php');
if(isset($OJ_LANG)){
    require_once("./lang/$OJ_LANG.php");
}

$view_title= $MSG_RANKLIST;

// 获取参数,排序规则
$scope="";
if(isset($_GET['scope'])) {
    $scope=$_GET['scope'];
}

if( $scope!="" && $scope!='d' && $scope!='w' && $scope!='m') {
    $scope='y';
}

// 获取参数用户ID
$prefix = '';
if(isset($_GET['prefix'])) {
    $prefix='%'.$_GET['prefix'].'%';
}

// 获取参数起始排名
$rank = 0;
if(isset( $_GET ['start'] )) {
    $rank = intval ( $_GET ['start'] );
}
if ( $rank < 0 ) {
    $rank = 0;
}

// 排名时不考虑不参与排名的账号
// 用户类型: 1 - 学生  2 - 老师  3 - 家长
$where=" where user_type=1 ";
if ( $prefix != '' ) {
    $where .= " and CONCAT(`user_id`,`school`) like ? ";
}
else {
    $where .= " and defunct='N' ";
}

// 每个榜单页包含的人数
$page_size = SysConfig::$RANKSIZE;
$sql = "SELECT `user_id`,`nick`, `school`, `solved`,`submit`, `accesstime` FROM `users` $where ORDER BY `solved` DESC, `submit` DESC  LIMIT  " .strval ( $rank ).",$page_size";
if( $scope ){
    $s=TimeUtil::ScopeTimeStr($scope);
    $sql = "select u.`user_id`, u.`nick`, u.`school`, u.`solved`, u.`submit`, s.last_in_date from users as u ";
    $sql .= " inner join ";
    $sql .= "(select user_id, max(in_date) as last_in_date from solution where result=4 GROUP BY user_id) as s ";
    $sql .= " on u.user_id = s.user_id and u.user_type=1 ";
    $sql .= " and last_in_date > str_to_date('$s','%Y-%m-%d') ";
    $sql .= " ORDER BY u.`solved` desc, u.submit desc";
    $sql .= " LIMIT  ".strval ($rank).", $page_size";
}

if(  $prefix != '' ){
    $result = pdo_query($sql, $prefix."%");
}
else{
    $result = pdo_query($sql);
}

if($result) {
    $rows_cnt=count($result);
}
else {
    $rows_cnt=0;
}
$view_rank=Array();
$i=0;
for ( $i=0;$i<$rows_cnt;$i++ ) {
    $row=$result[$i];
    $rank ++;
    $view_rank[$i][0]= $rank;
    $view_rank[$i][1]=  "<div class=center><a href='userinfo.php?user="
        .htmlentities ( $row['user_id'],ENT_QUOTES,"UTF-8")."'>"
        .htmlentities ( $row['nick'],ENT_QUOTES,"UTF-8")."</a>"."</div>";
    $view_rank[$i][2]=  "<div class=center>".htmlentities ( $row['school'],ENT_QUOTES,"UTF-8") ."</div>";
    $view_rank[$i][3]=  "<div class=center><a href='status.php?user_id="
        .htmlentities ( $row['user_id'],ENT_QUOTES,"UTF-8")
        ."&jresult=4'>" . $row['solved']."</a>"."</div>";
    $view_rank[$i][4]=  "<div class=center><a href='status.php?user_id="
        .htmlentities ($row['user_id'],ENT_QUOTES,"UTF-8")."'>"
        .$row['submit'] . "</a>"."</div>";
    // 统计正确率
    if ($row['submit'] == 0) {
        $view_rank[$i][5]= "00.00%";
    }
    else {
        $view_rank[$i][5]= sprintf ( "%.02lf%%", 100 * $row['solved'] / $row['submit'] );
    }
}

// 统计用户总数
$sql = "SELECT count(1) as `mycount` FROM `users`";
$result = pdo_query($sql);
$row=$result[0];
$view_total=$row['mycount'];

// 加载模板
require("template/".$OJ_TEMPLATE."/ranklist.php");

if(file_exists('./include/cache_end.php')) {
    require_once('./include/cache_end.php');
}
if ( file_exists("template/bs3/oj-footer.php") )
    require("template/bs3/oj-footer.php");
?>
